#include "RelayMotor.h"


GPIO_OUT_t RELAY_OUT[2];
void Relay_Gpio_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
	
	RCC_AHB1PeriphClockCmd(AXIS_CLK|CHARGE_CLK,ENABLE);

	/*********************************继电器*************************************/
	GPIO_InitStructure.GPIO_Pin = AXIS_PIN;//M3_DIR_PIN;   //A
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//下拉
	GPIO_Init(AXIS_PORT, &GPIO_InitStructure);//B
	
	GPIO_InitStructure.GPIO_Pin = CHARGE_PIN;           //E
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//下拉
	GPIO_Init(CHARGE_PORT, &GPIO_InitStructure);//B
	 
  RelayInit();
}




void RelayInit(void)
{
	RELAY_OUT[CHARGE_RELAY].GPIOx    = CHARGE_PORT;
	RELAY_OUT[CHARGE_RELAY].GPIO_Pin = CHARGE_PIN;
	RELAY_OUT[CHARGE_RELAY].Polarity = POS;
	
	RELAY_OUT[AXIS_RELAY].GPIOx      = AXIS_PORT;
	RELAY_OUT[AXIS_RELAY].GPIO_Pin   = AXIS_PIN;
	RELAY_OUT[AXIS_RELAY].Polarity   = POS;
	
	GPIO_Out(&RELAY_OUT[CHARGE_RELAY],CLOSE);
	GPIO_Out(&RELAY_OUT[AXIS_RELAY],CLOSE);
}

void GPIO_Out(GPIO_OUT_t *GpioOut,uint8_t RelayState)
{
	if(RelayState != TOG)
	{
		RelayState = (GpioOut->Polarity == NEG) ? (!RelayState) : (RelayState);
	}
	if(RelayState == CLOSE)
	{
		GPIO_ResetBits(GpioOut->GPIOx,GpioOut->GPIO_Pin);
	}
	else if(RelayState == OPEN)
	{
		GPIO_SetBits(GpioOut->GPIOx,GpioOut->GPIO_Pin);
	}
	else if(RelayState == TOG)
	{
		GPIO_ToggleBits(GpioOut->GPIOx,GpioOut->GPIO_Pin);
	}
}


